package com.fbee.smartcommunities.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fbee.smartcommunities.entity.RolePermission;
import com.fbee.smartcommunities.mapper.RolePermissionMapper;
import com.fbee.smartcommunities.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbee.smartcommunities.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 调整AUTO_INCREMENT值 服务实现类
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-16
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Resource
    private RolePermissionService rolePermissionService;

    @Resource
    private RoleService roleService;

    @Override
    @Transactional
    public Boolean add(Long[] resourcesIds, Boolean[] resourcesTypes, Long roleId) {
        // 先进行删除
        rolePermissionService.remove(new QueryWrapper<RolePermission>().eq("role_id", roleId));
        // 然后进行添加
        List<RolePermission> roleResources = new ArrayList<>();
        if(resourcesIds != null ){
            for (int i = 0 ;i < resourcesIds.length ;i++){
                roleResources.add(new RolePermission(roleId,resourcesIds[i],resourcesTypes[i]));
            }
        }
        return rolePermissionService.saveBatch(roleResources);
    }

}
